<template>
  <div class="host-box">
    <div class="host-tip"><b><span class="color-red">*</span>说明：</b> 服务级别配置优先级高于环境级别配置</div>
    <Form :model="hostForm" ref="hostForm" :rules="hostRules" :label-width="200" label-position="left">
      <FormItem label="容器CPU最小值(mCpu)" prop="requestCpu">
        <Input v-model.trim="hostForm.requestCpu" placeholder="100" @on-blur="updateBlur"></Input>
      </FormItem>
      <FormItem label="容器CPU最大值(mCpu)" prop="cpu">
        <Input v-model.trim="hostForm.cpu" placeholder="999" @on-blur="updateBlur"></Input>
      </FormItem>
      <FormItem label="容器内存最小值(Gi)" prop="requestMemory">
        <Input v-model.trim="hostForm.requestMemory" placeholder="0.5" @on-blur="updateBlur"></Input>
      </FormItem>
      <FormItem label="容器内存最大值(Gi)" prop="memory">
        <Input v-model.trim="hostForm.memory" placeholder="2" @on-blur="updateBlur"></Input>
      </FormItem>
    </Form>
  </div>
</template>
<script type="text/javascript">
export default {
  data() {
    const validateCpuRequest = (rule, value, callback) => {
      var regCpuR = /^\+?[1-9][0-9]*$/
      if (!value) {
        callback()
      } else if (!regCpuR.test(value)) {
        callback(new Error('请输入实数'))
      } else if (parseInt(value) > parseInt(this.hostForm.cpu) && this.hostForm.cpu !== '') {
        callback(new Error('cpu最小值不能大于cpu最大值'))
      } else {
        callback()
      }
    }
    const validateMemoryLimit = (rule, value, callback) => {
      // var regMemory = /^[1-9]\d*\.[5]$|0\.[5]$|^[1-9]\d*$/
      var regMemory = /^\d+(\.\d+)?$/
      if (!value) {
        callback()
      } else if (!regMemory.test(value) || parseFloat(value) < parseFloat(this.hostForm.requestMemory)) {
        // callback(new Error('请输入实数，0.5的倍数，大于内存最小值'))
        callback(new Error('请输入实数，大于内存最小值'))
      } else {
        callback()
      }
    }
    const validateMemoryRequest = (rule, value, callback) => {
      // var regMemoryR = /^[1-9]\d*\.[5]$|0\.[5]$|^[1-9]\d*$/
      var regMemoryR = /^\d+(\.\d+)?$/
      if (!value) {
        callback()
      } else if (!regMemoryR.test(value) || (parseFloat(value) > parseFloat(this.hostForm.memory) && this.hostForm.memory !== '')) {
        // callback(new Error('请输入实数，0.5的倍数，小于内存最大值'))
        callback(new Error('请输入实数，小于内存最大值'))
      } else {
        callback()
      }
    }
    const validateCpuLimit = (rule, value, callback) => {
      var regCpu = /^\+?[1-9][0-9]*$/
      if (!value) {
        callback()
      } else if (!regCpu.test(value)) {
        callback(new Error('请输入实数'))
      } else if (parseInt(value) < parseInt(this.hostForm.requestCpu)) {
        callback(new Error('cpu最大值不能小于cpu最小值'))
      } else {
        callback()
      }
    }
    return {
      hostRules: {
        requestCpu: [
          { required: false, trigger: 'blur', validator: validateCpuRequest }
        ],
        requestMemory: [
          { required: false, trigger: 'blur', validator: validateMemoryRequest }
        ],
        cpu: [
          { required: false, trigger: 'blur', validator: validateCpuLimit }
        ],
        memory: [
          { required: false, trigger: 'blur', validator: validateMemoryLimit }
        ]
      }
    }
  },
  props: {
    hostForm: {
      type: Object,
      default: {
        requestCpu: '',
        cpu: '',
        requestMemory: '',
        memory: ''
      }
    }
  },
  methods: {
    updateBlur() {
      this.$refs.hostForm.validate((valid) => {
        if (valid) {
          this.$emit('update:hostForm', {
            requestCpu: this.hostForm.requestCpu,
            cpu: this.hostForm.cpu,
            requestMemory: this.hostForm.requestMemory,
            memory: this.hostForm.memory,
            hostValide: true
          })
        } else {
          this.$emit('update:hostForm', {
            requestCpu: this.hostForm.requestCpu,
            cpu: this.hostForm.cpu,
            requestMemory: this.hostForm.requestMemory,
            memory: this.hostForm.memory,
            hostValide: false
          })
        }
      })
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.host-box {
  div.ivu-form-item {
    margin-bottom: 14px;
  }
}
.host-tip {
  margin-bottom: 10px;
  margin-left: 30px;
  color: #999;
}
.host-box /deep/ .ivu-form-item-error-tip {
  padding-top: 1px;
}
</style>
